﻿using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;

namespace BBMS.DLL.DataContract
{
    public class NhanVienQuay : BBMS.DLL.Base.ObjectBase
    {
        public Int32 NVID;
        public List<Quay> MaQuay;
        public NhanVienQuay()
        {
        }

        public NhanVienQuay(Int32 nvid)
        {
            this.NVID = nvid;
            this.MaQuay = GetDSQuay();
        }

        public List<Quay> GetDSQuay()
        {
            List<Quay> q = new List<Quay>();
             
            try
            {
                MySqlConnection con = new MySqlConnection(Base.Config.ConnectionString());
                con.Open();
                MySqlCommand cmd = con.CreateCommand();
                cmd.CommandText = "select a.MaQuay,b.Ten from " + this.TableName + " a left join counter  b on a.NVID = @NVID and a.MaQuay = b.Ma ";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@NVID", this.NVID);
                MySqlDataReader rd = cmd.ExecuteReader();

                while (rd.Read())
                    q.Add(new Quay(rd["MaQuay"].ToString(), rd["Ten"].ToString(), "", ""));
                    //arrRet.Add(new KeyValuePair(rd["Ma"].ToString(), rd["Ten"].ToString()));
                // this.Add(nv.DataReaderToObject(rd));

                rd.Close();
                rd.Dispose();
                con.Close();
                con.Dispose();
            }
            catch (Exception ex)
            {

            }

            return q;
        }

        public NhanVienQuay(Int32 nvid, List<Quay> maquay)
        {
            this.NVID = nvid;
            this.MaQuay = maquay;
        }  
     
        public override string TableName
        {
            get
            {
                return "usercounter";
            }
        }

        public override string[] KeyFields
        {
            get
            {
                return new string[] { "NVID", "MaQuay" };
            }
        }

        public override string[] IDFields
        {
            get
            {
                return new string[] { "NVID", "MaQuay" };
            }
        }

        public override string InsertNew()
        {
            //return base.InsertNew();
            string msg = "";
            MySqlTransaction tn = null;
            try
            {
                MySqlConnection con = new MySqlConnection(Base.Config.ConnectionString());
                con.Open();
                tn = con.BeginTransaction();
                MySqlCommand cmd1 = con.CreateCommand();
                cmd1.CommandType = System.Data.CommandType.Text;
                cmd1.CommandText = "delete from " + this.TableName + " where NVID = @NVID";
                cmd1.Parameters.AddWithValue("@NVID", this.NVID);
                cmd1.Transaction = tn;
                cmd1.ExecuteNonQuery();
                for (int i = 0; i < this.MaQuay.Count; i++)
                {
                    MySqlCommand cmd = con.CreateCommand();
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.CommandText = "insert into " + this.TableName + " (NVID,MaQuay) values (@NVID,@MaQuay)";
                    cmd.Transaction = tn;
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@NVID", this.NVID);
                    cmd.Parameters.AddWithValue("@MaQuay", this.MaQuay[i].Ma);                    
                    cmd.ExecuteNonQuery();                    
                    //cmd.CommandText = "select count(*) from " + this.TableName + " where NVID = @NVID and MaQuay = @MaQuay";
                    //cmd.Transaction = tn;
                    //cmd.Parameters.Clear();
                    //cmd.Parameters.AddWithValue("@NVID", this.NVID);
                    //cmd.Parameters.AddWithValue("@MaQuay", this.MaQuay[i].Ma);
                    //long count = (long)cmd.ExecuteScalar();
                    //if (count != 0) { }
                    //else
                    //{
                    //    cmd.CommandText = "insert into " + this.TableName + " (NVID,MaQuay) values (@NVID,@MaQuay)";
                    //    cmd.ExecuteNonQuery();                     
                    //}

                }
                tn.Commit();
                msg = "";
                con.Close();
                con.Dispose();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.StackTrace);
                msg = "Lỗi xử lý cơ sở dữ liệu.";
                tn.Rollback();
            }
            finally
            {

                tn.Dispose();
            }
            return msg;
            

                
        }
       
    }
}
